
package formulario;

import java.sql.*;
import java.sql.PreparedStatement;
import java.sql.ResultSetMetaData;
import java.util.*;
import javax.swing.JOptionPane;

public class cuestionario extends javax.swing.JFrame {
    String nombre, codigo;
    private Connection conn;
    private Connection connami;
    int cl;
    String yuca;
    ResultSet res=null;
    ResultSet resultante=null;
    ResultSet ultimo=null;
    Statement st=null;
    Statement estado=null;
    String acer;
    int resuser;
    int nfilas=0;
    int conta=0;
    double cvalorp;
    double acum;
    double rescor;
    double valorp;
    int correcta=0;
    Boolean existe;
    /** Creates new form cuestionario */
    public cuestionario() {
        initComponents();
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        grupobuton = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        siguiente = new javax.swing.JButton();
        r1 = new javax.swing.JRadioButton();
        r2 = new javax.swing.JRadioButton();
        r3 = new javax.swing.JRadioButton();
        r4 = new javax.swing.JRadioButton();
        pregunta = new javax.swing.JTextField();
        respuesta1 = new javax.swing.JTextField();
        respuesta2 = new javax.swing.JTextField();
        respuesta3 = new javax.swing.JTextField();
        respuesta4 = new javax.swing.JTextField();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        grupobuton.add(r1);
        grupobuton.add(r2);
        grupobuton.add(r3);
        grupobuton.add(r4);

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Prueba");

        jLabel1.setFont(new java.awt.Font("Centaur", 1, 14)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(51, 51, 0));
        jLabel1.setText("Enunciado");

        siguiente.setFont(new java.awt.Font("Centaur", 1, 14)); // NOI18N
        siguiente.setForeground(new java.awt.Color(51, 0, 51));
        siguiente.setText("Siguiente ");
        siguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SIGUIENTE(evt);
            }
        });

        r1.setFont(new java.awt.Font("Centaur", 1, 14)); // NOI18N
        r1.setForeground(new java.awt.Color(51, 51, 0));
        r1.setText("1.");

        r2.setFont(new java.awt.Font("Centaur", 1, 14)); // NOI18N
        r2.setForeground(new java.awt.Color(51, 51, 0));
        r2.setText("2.");

        r3.setFont(new java.awt.Font("Centaur", 1, 14)); // NOI18N
        r3.setForeground(new java.awt.Color(51, 51, 0));
        r3.setText("3.");

        r4.setFont(new java.awt.Font("Centaur", 1, 14)); // NOI18N
        r4.setForeground(new java.awt.Color(51, 51, 0));
        r4.setText("4.");

        pregunta.setEditable(false);

        respuesta1.setEditable(false);

        respuesta2.setEditable(false);

        respuesta3.setEditable(false);

        respuesta4.setEditable(false);

        jMenu1.setText("File");

        jMenu3.setText("Salir");
        jMenu1.add(jMenu3);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(r3)
                    .addComponent(r2)
                    .addComponent(jLabel1)
                    .addComponent(r4)
                    .addComponent(r1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(respuesta2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 391, Short.MAX_VALUE)
                    .addComponent(respuesta4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 391, Short.MAX_VALUE)
                    .addComponent(pregunta, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 391, Short.MAX_VALUE)
                    .addComponent(respuesta3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 391, Short.MAX_VALUE)
                    .addComponent(respuesta1, javax.swing.GroupLayout.DEFAULT_SIZE, 391, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(208, Short.MAX_VALUE)
                .addComponent(siguiente)
                .addGap(180, 180, 180))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(pregunta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(respuesta1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(r1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(respuesta2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(r2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(respuesta3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(r3))
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(respuesta4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(r4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(siguiente)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void SIGUIENTE(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SIGUIENTE
// TODO add your handling code here:   
     if(r1.isSelected()||r2.isSelected()||r3.isSelected()||r4.isSelected())
    {
      if(r1.isSelected())
      {
          resuser=1;
      }
      else if(r2.isSelected())
      {
          resuser=2;
      }
      else if(r3.isSelected())
      {
          resuser=3;
      }
      else
      {
          resuser=4;
      }
        if(resuser==rescor)
       {
         acum= acum+(5*valorp);
         correcta++;
       } 
     grupobuton.clearSelection();   
     questions();
    }
     else 
    {
        JOptionPane.showMessageDialog(null, "Por favor seleccione una respuesta","Error",JOptionPane.ERROR_MESSAGE);
    }
  
}//GEN-LAST:event_SIGUIENTE
public void setdatos(String n, String c, String pal, int ubi, String columna, Boolean exi)
    {
    nombre=n;
    codigo=c;
    cl=ubi;
    acer=columna;
    existe=exi;
        
    try
    { 
    yuca = pal;
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    conn = DriverManager.getConnection("jdbc:odbc:alex");
    st=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    connami = DriverManager.getConnection("jdbc:odbc:alex");
    estado=connami.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
    resultante = estado.executeQuery("SELECT * FROM "+yuca);
    
    while(resultante.next()){
    cvalorp=cvalorp+resultante.getInt(8);
                            }
    resultante.beforeFirst();
  
    res=st.executeQuery("SELECT * FROM "+yuca);
    res.last();
    nfilas=res.getRow();
    res.first();
    questions();
  
  
    }catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
public void questions(){     
 try
    {  
      resultante.next();             
      if(conta==nfilas){
       if(existe==false)
       {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      conn = DriverManager.getConnection("jdbc:odbc:alex");
      PreparedStatement st = conn.prepareStatement("INSERT INTO usuario (`nombre`,`codigo`,`"+acer+"`) VALUES (?,?,?)");
            acum=acum/cvalorp;
            st.setString(1,nombre);
            st.setString(2,codigo);
            st.setDouble(3,acum);
            
            JOptionPane.showMessageDialog(null, "Prueba terminada, su calificacion es: "+acum+" sobre 5 y tuvo "+correcta+" respuestas correctas","RESULTADO",JOptionPane.INFORMATION_MESSAGE);   
            st.executeUpdate();
            acum=0;
            cvalorp=0;
            correcta=0;
            conn.close();
            connami.close();
            conta=0;
            this.dispose();                     
       }else
       {
           Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            conn = DriverManager.getConnection("jdbc:odbc:alex");
            PreparedStatement st=conn.prepareStatement("UPDATE usuario SET "+acer+" = ? where codigo = "+codigo+"");
            acum=acum/cvalorp;
         
            st.setDouble(1,acum);
            
            JOptionPane.showMessageDialog(null, "Prueba terminada, su calificacion es: "+acum+" sobre 5 y tuvo "+correcta+" respuestas correctas","RESULTADO",JOptionPane.INFORMATION_MESSAGE);
            st.executeUpdate();
            acum=0;
            cvalorp=0;
            correcta=0;
            conn.close();
            connami.close();
            conta=0;
            this.dispose(); 
       }
      }
      else{                                       
            pregunta.setText(res.getString(2));
            respuesta1.setText(res.getString(3));
            respuesta2.setText(res.getString(4));
            respuesta3.setText(res.getString(5));
            respuesta4.setText(res.getString(6));
            rescor=resultante.getInt(7);
            valorp=resultante.getInt(8);
            res.next(); 
            conta++;                
         }
        
   
    }catch(Exception ex)
        {
            ex.printStackTrace();
        }    
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(cuestionario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(cuestionario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(cuestionario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(cuestionario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new cuestionario().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup grupobuton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JTextField pregunta;
    private javax.swing.JRadioButton r1;
    private javax.swing.JRadioButton r2;
    private javax.swing.JRadioButton r3;
    private javax.swing.JRadioButton r4;
    private javax.swing.JTextField respuesta1;
    private javax.swing.JTextField respuesta2;
    private javax.swing.JTextField respuesta3;
    private javax.swing.JTextField respuesta4;
    private javax.swing.JButton siguiente;
    // End of variables declaration//GEN-END:variables
}
